window.onload=function (){
	var that = this;
	this.getStuList();
	this.getTecList();
	this.getCorList();
}

function onSubmitStu(){
	var name = document.getElementById("name").value;
	var sno = document.getElementById("sno").value;
	var gender = document.getElementById("gender").value;
	var college = document.getElementById("college").value;
	var classes = document.getElementById("classes").value;
	var pw = document.getElementById("pw").value;
	if(gender == "女"){
		gender = 0;
	} else {
		gender = 1;
	}
	if(name.length * sno.length * gender.length * college.length * classes.length * pw.length == 0){
		alert("输入内容不能为空！");
	} else {
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/users",
			type:'POST',
			dataType: 'JSON',
			contentType:'application/json',
			data : JSON.stringify({
				name : name,
				sno : sno,
				password : pw,
				gender : gender,
				college : college,
				classes : classes,
				isAdmin : 0,
				isTeacher : 0,
			}),
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	document.getElementById("name").value = "";
				document.getElementById("sno").value = "";
				document.getElementById("gender").value = "";
				document.getElementById("college").value = "";
				document.getElementById("classes").value = "";
				document.getElementById("pw").value = "";
	        	alert("添加成功！");
	        }
		})
	}
}

function getStuList(){
	$.ajax({
		url:"https://dlnu-ljq.cn.utools.club/users/student",
		type:'GET',
		dataType: 'JSON',
		contentType:'application/json',
        crossDomain: true,
        asyn:"false",
        success:function(res){
        	console.log(res);
        	var content = "<tr>";
        	for(var i = 0; i < res.data.length; i++){
        		var gender = "男";
        		if(res.data[i].gender == 0){
        			gender = "女"
        		}
        		content += "<td>" + res.data[i].name + "</td>";
        		content += "<td>" + res.data[i].sno + "</td>";
        		content += "<td>" + gender + "</td>";
        		content += "<td>" + res.data[i].college + "</td>";
        		content += "<td>" + res.data[i].classes + "</td>" + "<tr>";
        	}
        	$('#total-tr').html(content);
        }
	})
}

function updateUserInfo(){
	var name = document.getElementById("name1").value;
	var sno = document.getElementById("sno1").value;
	var gender = document.getElementById("gender1").value;
	var college = document.getElementById("college1").value;
	var classes = document.getElementById("classes1").value;
	if(gender == "女"){
		gender = 0;
	} else {
		gender = 1;
	}
	if(sno.length == 0){
		alert("学号不能为空！");
	} else {
		if(name.length == 0){
			name = null;
		}
		if(gender.length == 0){
			gender = null;
		}
		if(college.length == 0){
			college = null;
		}
		if(classes.length == 0){
			classes = null;
		}
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/users",
			type:'PUT',
			dataType: 'JSON',
			contentType:'application/json',
			data : JSON.stringify({
				name : name,
				sno : sno,
				gender : gender,
				college : college,
				classes : classes,
				isAdmin : 0,
				isTeacher : 0,
			}),
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	console.log(res);
	        	document.getElementById("name1").value = "";
				document.getElementById("sno1").value = "";
				document.getElementById("gender1").value = "";
				document.getElementById("college1").value = "";
				document.getElementById("classes1").value = "";
	        	alert("修改成功！");
	        }
		})
	}
}

function deleteUserInfo(){
	var sno = document.getElementById("sno2").value;
	if(sno.length == 0){
		alert("输入内容不能为空！");
	} else {
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/users/" + sno,
			type:'DELETE',
			dataType: 'JSON',
			contentType:'application/json',
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	console.log(res);
				document.getElementById("sno2").value = "";
	        	alert("删除成功！");
	        }
		})
	}
}

function getTecList(){
	$.ajax({
		url:"https://dlnu-ljq.cn.utools.club/users/teacher",
		type:'GET',
		dataType: 'JSON',
		contentType:'application/json',
        crossDomain: true,
        asyn:"false",
        success:function(res){
        	console.log(res);
        	var content = "<tr>";
        	for(var i = 0; i < res.data.length; i++){
        		var gender = "男";
        		if(res.data[i].gender == 0){
        			gender = "女"
        		}
        		content += "<td>" + res.data[i].name + "</td>";
        		content += "<td>" + res.data[i].sno + "</td>";
        		content += "<td>" + gender + "</td>";
        		content += "<td>" + res.data[i].college + "</td>" + "<tr>";
        	}
        	$('#total-tr-tec').html(content);
        }
	})
}

function onSubmitTec(){
	var name = document.getElementById("tname").value;
	var sno = document.getElementById("tno").value;
	var gender = document.getElementById("tgender").value;
	var college = document.getElementById("tcollege").value;
	var pw = document.getElementById("tpw").value;
	if(gender == "女"){
		gender = 0;
	} else {
		gender = 1;
	}
	if(name.length * sno.length * gender.length * college.length * classes.length * pw.length == 0){
		alert("输入内容不能为空！");
	} else {
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/users",
			type:'POST',
			dataType: 'JSON',
			contentType:'application/json',
			data : JSON.stringify({
				name : name,
				sno : sno,
				password : pw,
				gender : gender,
				college : college,
				classes : 0,
				isAdmin : 0,
				isTeacher : 1,
			}),
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	console.log(res);
	        	document.getElementById("tname").value = "";
				document.getElementById("tno").value = "";
				document.getElementById("tgender").value = "";
				document.getElementById("tcollege").value = "";
				document.getElementById("tpw").value = "";
	        	alert("添加成功！");
	        }
		})
	}
}

function updateTecInfo(){
	var name = document.getElementById("tname1").value;
	var sno = document.getElementById("tno1").value;
	var gender = document.getElementById("tgender1").value;
	var college = document.getElementById("tcollege1").value;
	if(gender == "女"){
		gender = 0;
	} else {
		gender = 1;
	}
	if(sno.length == 0){
		alert("学号不能为空！");
	} else {
		if(name.length == 0){
			name = null;
		}
		if(gender.length == 0){
			gender = null;
		}
		if(college.length == 0){
			college = null;
		}
		var classes = 0;
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/users",
			type:'PUT',
			dataType: 'JSON',
			contentType:'application/json',
			data : JSON.stringify({
				name : name,
				sno : sno,
				gender : gender,
				college : college,
				classes : classes,
				isAdmin : 0,
				isTeacher : 1,
			}),
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	console.log(res);
	        	document.getElementById("tname1").value = "";
				document.getElementById("tno1").value = "";
				document.getElementById("tgender1").value = "";
				document.getElementById("tcollege1").value = "";
	        	alert("修改成功！");
	        }
		})
	}
}

function deleteTecInfo(){
	var sno = document.getElementById("tno2").value;
	if(sno.length == 0){
		alert("输入内容不能为空！");
	} else {
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/users/" + sno,
			type:'DELETE',
			dataType: 'JSON',
			contentType:'application/json',
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	console.log(res);
				document.getElementById("tno2").value = "";
	        	alert("删除成功！");
	        }
		})
	}
}

function getCorList(){
	$.ajax({
		url:"https://dlnu-ljq.cn.utools.club/course",
		type:'GET',
		dataType: 'JSON',
		contentType:'application/json',
        crossDomain: true,
        asyn:"false",
        success:function(res){
        	console.log("---------");
        	console.log(res);
        	var content = "<tr>";
        	for(var i = 0; i < res.data.length; i++){
        		content += "<td>" + res.data[i].courseName + "</td>";
        		content += "<td>" + res.data[i].courseId + "</td>";
        		content += "<td>" + res.data[i].maxCount + "</td>";
        		content += "<td>" + res.data[i].nowCount + "</td>";
        		content += "<td>" + res.data[i].credit + "</td>";
        		content += "<td>" + res.data[i].teacherName + "</td>" + "<tr>";
        	}
        	$('#total-tr-cor').html(content);
        }
	})
}

function onSubmitCor(){
	var cname = document.getElementById('cname').value;
	var cno = document.getElementById('cno').value;
	var maxCount = document.getElementById('maxCount').value;
	var nowCount = document.getElementById('nowCount').value;
	var credit = document.getElementById('credit').value;
	var teacher = document.getElementById('teacher').value;
	var corArr = document.getElementById('corArr').value;
	var weeks = document.getElementById('weeks').value;
	var week = document.getElementById('week').value;
	var session = document.getElementById('session').value;
	var sectionNumber = document.getElementById('sectionNumber').value;
	var teachingBuilding = document.getElementById('teachingBuilding').value;
	var classroom = document.getElementById('classroom').value;
	var weeksInterval = document.getElementById('weeksInterval').value;
	if(cname.length * cno.length * maxCount.length * nowCount.length * credit.length * teacher.length * corArr.length * weeks.length * week.length * session.length * sectionNumber.length * teachingBuilding.length * classroom.length * weeksInterval.length == 0){
		alert("输入内容不能为空！");
	} else {
		if(corArr == "通识教育"){
			corArr = 0;
		} else {
			corArr = 1;
		}
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/course",
			type:'POST',
			dataType: 'JSON',
			contentType:'application/json',
			data : JSON.stringify({
				courseName : cname,
				courseId : cno,
				maxCount : maxCount,
				nowCount : nowCount,
				credit : credit,
				teacherName : teacher,
				courseAttribute : corArr,
				weeks : weeks,
				week : week,
				session : session,
				sectionNumber : sectionNumber,
				teacherBuilding : teachingBuilding,
				classroom : classroom,
				weeksInterval : weeksInterval,
				courseId : cno,
			}),
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	console.log(res);
	        	document.getElementById('cname').value = "";
				document.getElementById('cno').value = "";
				document.getElementById('maxCount').value = "";
				document.getElementById('nowCount').value = "";
				document.getElementById('credit').value = "";
				document.getElementById('teacher').value = "";
				document.getElementById('corArr').value = "";
				document.getElementById('weeks').value = "";
				document.getElementById('week').value = "";
				document.getElementById('session').value = "";
				document.getElementById('sectionNumber').value = "";
				document.getElementById('teachingBuilding').value = "";
				document.getElementById('classroom').value = "";
				document.getElementById('weeksInterval').value = "";
	        	alert("添加成功！");
	        }
		})
	}
}

function deleteCorInfo(){
	var cno = document.getElementById('cno1').value;
	if(cno.length == 0){
		alert("输入内容不能为空！");
	} else {
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/course/" + cno,
			type:'DELETE',
			dataType: 'JSON',
			contentType:'application/json',
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	console.log(res);
				document.getElementById("cno1").value = "";
	        	alert("删除成功！");
	        }
		})
	}
}

function onSearchCor(){
	var cno = document.getElementById('cno2').value;
	if(cno.length == 0){
		alert("输入内容不能为空！");
	} else {
		$.ajax({
			url: "https://dlnu-ljq.cn.utools.club/course/single/" + cno,
			type:'GET',
			dataType: 'JSON',
			contentType:'application/json',
	        crossDomain: true,
	        asyn:"false",
	        success:function(res){
	        	console.log(res);
	        	var content = "<tr>";
	        	content += "<td>" + res.data.courseName + "</td>";
        		content += "<td>" + res.data.courseId + "</td>";
        		content += "<td>" + res.data.maxCount + "</td>";
        		content += "<td>" + res.data.nowCount + "</td>";
        		content += "<td>" + res.data.credit + "</td>";
        		content += "<td>" + res.data.teacherName + "</td>" + "<tr>";
				document.getElementById("cno2").value = "";
				$('#total-tr-cor').html(content);
	        }
		})
	}
}